import Vue from 'vue'
const request = (params) => {
	let $website = {
		url: 'http://120.25.230.253:9090',
	}
	let _self = new Vue()
	let url = $website.url + params.url
	let method = params.method || 'GET'
	let data = params.data || {}
	let showLoading = params.showLoading == undefined ? true : params.showLoading
	let api = params.api == undefined ? "" : params.api
	let token = uni.getStorageSync('Authorization')
	// sessionStorage.getItem('Authorization')

	let header = params.header == undefined ? {
		'X-Requested-With': 'XMLHttpRequest'
	} : params.header;
	if (params.contentType) {
		params.header['content-type'] = params.contentType
	}
	if (showLoading) {
		uni.showLoading({
			mask: true,
			title: (params.showLoadingTitle ? params.showLoadingTitle : "加载中...")
		});
	}
	if (!header.hasOwnProperty('Authorization') && token) {
		header.Authorization = token;
	}
	return new Promise((resolve, reject) => {
		uni.request({
			url: url,
			data: data,
			header: header,
			method: method,
		}).then(res => {
			let {
				data: dataType
			} = res[1]
			if (res[1] && res[1].statusCode === 200) {
				resolve(dataType)
			}
		}).catch(err => { // 错误
			reject(err)
		}).finally(() => {
			if (showLoading) {
				uni.hideLoading();
			}
		})
	})
}
export default request
